using UnityEngine;
using UnityEngine.UI;

namespace Gj
{
    public class CoolTimeHelper : NodeHelper
    {
        private Image _Image;
        private bool ing;
        private float startTime;
        private float time;
        private float totalTime;

        private Image Image
        {
            get
            {
                if (_Image == null) _Image = GetComponent<Image>();
                return _Image;
            }
        }

        protected override BindDataType bindDataType => BindDataType.Float;

        private void Update()
        {
            if (!ing) return;
            if (Time.time - startTime > totalTime)
            {
                ing = false;
                Image.fillAmount = 0;
                Image.gameObject.SetActive(false);
            }

            Image.fillAmount = 1 - (Time.time - startTime) / totalTime;
        }

        protected override void Init()
        {
            base.Init();
            Image.gameObject.SetActive(false);
        }

        protected override void BindData(float t)
        {
            if (time == t) return;
            time = t;
            totalTime = time - Time.time;
            startTime = Time.time;
            ing = true;
            Image.gameObject.SetActive(true);
        }
    }
}